import { getTop, getOrderAmount, getOrderCount, getTaskReport } from '@/api/shopping'
const moment = require('moment')
export default {
  namespaced: true,
  state () {
    return {
      topList: [],
      start: moment().startOf('month').format('YYYY-MM-DD+00:00:00'),
      end: moment().subtract(0, 'days').format('YYYY-MM-DD+23:59:59'),
      orderCount: '',
      orderAmount: '',
      taskReports: {
        cancelTotal: 0,
        completedTotal: 0,
        progressTotal: 0,
        total: 0
      }
    }
  },
  mutations: {
    toplists (state, payload) {
      state.topList = payload
    },

    orderamount (state, payload) {
      state.orderAmount = payload
    },
    ordercount (state, payload) {
      state.orderCount = payload
    },
    taskNum (state, payload) {
      payload.forEach(item => {
        state.taskReports.total += item.total
        state.taskReports.cancelTotal += item.cancelTotal
        state.taskReports.progressTotal += item.progressTotal
        state.taskReports.completedTotal += item.completedTotal
      })
    }
  },
  actions: {
    async getTopShoppings (context, payload) {
      const res = await getTop(`${payload[0]}/${payload[1]}`)
      context.commit('toplists', res)
    },
    async getAmount (context) {
      const res = await getOrderAmount(context.state.start, context.state.end)
      context.commit('orderamount', res)
    },
    async getCmount (context) {
      const res = await getOrderCount(context.state.start, context.state.end)
      context.commit('ordercount', res)
    },
    async getTask (context, payload) {
      const res = await getTaskReport(payload[0], payload[1])
      context.commit('taskNum', res)
    }
  }
}
